#!/usr/bin/perl 

use strict;
use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi();
my $form_ref = parse_input();
print_header();

my %FORM_DATA = %$form_ref; 

my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $activity = $FORM_DATA{'activity'};

my $week = get_current_week();

# print "NUM: $num NAME: $name ACTIVITY: $activity<br/><br/>";

my ($now, $now_date, $now_time, $logged_in, $stmnt);

# print "activity is $activity<br/>";

if ($activity == 1 ) {
    select_payroll_period($num, $name, $job, $loc); ; 
} elsif ($activity == 2 ) {
	select_date_range ($num, $name, $job, $loc); 
} elsif ($activity == 3 ) {
	add_new_perm ($num, $name, $job, $loc);
} elsif ($activity == 4 ) {	
	add_new_temp ($num, $name, $job, $loc);
} elsif ($activity == 5 ) {
   select_employee_to_modify ($num, $name, $job, $loc);
} elsif ($activity == 6 ) {
 	close_pay_dates ($num, $name, $job, $loc, $activity);
} elsif ($activity == 7 ) {
 	open_pay_dates ($num, $name, $job, $loc, $activity);
} elsif ($activity > 100 ) {
 #    & review_payroll_week ($activity);	
} else {
#   & return_to_activity
}

db_disconnect();

############### SUBROUTINES FOLLOW ###############

sub select_payroll_period
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $date;
 	my @payPeriods=();		
	my $payPeriod_ref = get_payroll_periods();
   @payPeriods = @$payPeriod_ref;

	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3>Generate Payroll Table</h3> 
	<div id='box1'>
	Employee Number:<br/><br/>
	Employee Name:<br/><br/>
	Select Pay Period:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_pay_generate_payroll.pl' method='post'>
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<select id=payPeriod name="payPeriod" tabindex=1 >
ENDHEADER

for my $i (0..$#payPeriods ) {
   print "<option value= $payPeriods[$i][0] >$payPeriods[$i][1]  thru  $payPeriods[$i][2]</option>";
}
print <<ENDHEADER;
</select>
<br/><br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER

}


sub select_date_range
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $date;
 	my @agency=();		
	my $agency_ref = get_agencies();
 	my @facility=();		
	my $facility_ref = get_facilities();
   	@agency = @$agency_ref;
	@facility = @$facility_ref;

	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3>Generate Agency Hours Report</h3>
	<br/>
	<form id='box1Left' autocomplete='off' name='selectAgency' action='${cgi_ref}trak_03_pay_generate_agency.pl' method='post'>
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<table>
		<tr>
			<td>Start Date:<td/>
			<td><input type="text" size=10 name=dateStart /><td/>
			<td id='info'> &nbsp YYYY-MM-DD<td/>
		<tr/>
		<tr>	 
			<td>End Date:<td/>
			<td><input type="text" size=10 name=dateEnd /><td/>
		<tr/>
		<tr>
			<td>Agency:<td/>
			<td><select id=agencyId name="agencyID" tabindex=1 >
ENDHEADER
for my $i (0..$#agency ) {
   print "<option value= $agency[$i] >$agency[$i] </option>";
}
print <<ENDHEADER;
			<td/>
		<tr/>
			<td>Facility:<td/>
			<td><select id=facilityId name="facilityID" tabindex=1 >
ENDHEADER
for my $j (0..$#facility ) {
   print "<option value= $facility[$j][0] >$facility[$j][1] </option>";
}
print <<ENDHEADER;
			<td/>
		<tr/>
	<table/>	
	<br/>
   <input type="submit" value="Generate Agency Report">
	</div>
   <br/><br/>
   <a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
   <br/><br/>
	</form>
	</body>
	</html>
ENDHEADER

}


sub add_new_perm
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];

print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_02_sup_activity_select_add_new_temp</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <h3>Add New Permanent Employee</h3>
	<form id="box1left" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_perm.pl" method="post">
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<table>
		<tr>
			<td>First Name: &nbsp</td>
			<td><input type="text" size=24 name=prmFirst /></td>
		</tr>
		<tr>
			<td>Last Name: </td>
			<td><input type="text" size=24 name=prmLast /></td>
		</tr>
		<tr>
		   <td>Login ID: </td>
		   <td><input type="text" size=10 name=prmLogin /></td>
		</tr>
		<tr>
			<td>Location: </td>
			<td><label id=info ><input type="text" size=5 name=prmLocation />&nbsp VAN or TOR</label></td>
		</tr>
		<tr>
   		<td>Facility #:</td>
   		<td><input type="text" size=5 name=prmFacility /></td>
   	<tr/>
   	<tr>
	   	<td>Pay Cat: </td>
	   	<td><input type="text" size=5 name=prmPayCat /></td>
		</tr>
   	<tr>
	   	<td>Job: </td>
	   	<td><input type="text" size=5 name=prmJob /></td>
		</tr>
		<tr>
   		<td>Rate: </td>
   		<td><input type="text" size=5 name=prmRate /><td>
		</tr>
		<tr>
   		<td>Emp #: </td>
   		<td><input type="text" size=5 name=prmNum /><td>
		</tr>
	</table>
	<br/>
   <input type="submit" value="Add New Permanent">
   <br/><br/>
	<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
	<br/><br/>
 	</form>
	</body>
 	</html>
ENDHEADER

}


sub add_new_temp
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];

print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_02_sup_activity_select_add_new_temp</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <h3>Add New Temp Employee</h3>
ENDHEADER
print <<ENDHEADER;
<form id="box1Left" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_temp.pl" method="post">
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<table>
		<tr>
			<td>First Name: &nbsp</td>
			<td><input type="text" size=24 name=tmpFirst /></td>
		</tr>
		<tr>
			<td>Last Name: </td>
			<td><input type="text" size=24 name=tmpLast /></td>
		</tr>
		<tr>
		   <td>Login ID: </td>
		   <td><input type="text" size=10 name=tmpLogin /></td>
		</tr>
		<tr>
			<td>Location: </td>
			<td><label id=info ><input type="text" size=5 name=tmpLocation />&nbsp VAN or TOR</label></td>

		</tr>
		<tr>
   		<td>Facility #:</td>
   		<td><input type="text" size=5 name=tmpFacility /></td>
   	<tr/>
   	<tr>
	   	<td>Agency: </td>
	   	<td><input type="text" size=5 name=tmpAgency /></td>
		</tr>
		<tr>
   		<td>Rate: </td>
   		<td><input type="text" size=5 name=tmpRate /><td>
		</tr>
	</table>
	<br/>
	<input type="submit" value="Add New Temp">
	<br/><br/>
	<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
 	</form>
 	</body>
 	</html>
ENDHEADER
}

sub select_employee_to_modify
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_adm_activity.html</title>
</head>
<body onload='startUp1()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number:<br/><br/>
Employee Name:<br/><br/>
</div>
<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_modify_employee_data.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>

<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<table>
	<tr>
		<td>Enter Employee Num: &nbsp</td>
		<td><input type="text" size=5 name=modNum /></td>
	</tr>
</table>
<br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/><br/>
<a href="${cgi_ref}trak_03_get_employee_list.pl?loc=$loc" target="_blank">Get Employee List<a/>
<br/><br/>
<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER

}

sub close_pay_dates 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $date;
 	my @payDates=();		
	my $payDates_ref = get_payroll_dates();
    @payDates = @$payDates_ref;

	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body onload='startUp1()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3> Close Payroll Dates</h3>
	<div id='box1'>
	Employee Number:<br/><br/>
	Employee Name:<br/><br/>
	Select Date:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_pay_close_dates.pl' method='post'>
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<select id=payDate name="payDate" tabindex=1 >
ENDHEADER
foreach $date (@payDates) {
   print "<option value=$date >$date</option>";
}
print <<ENDHEADER;
</select>
<br/><br/>
<input type='submit' name='submit' value='Close Dates'>
<br/><br/>
<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER

}

sub open_pay_dates 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];	my $date;
 	my @payDates=();		
	my $payDates_ref = get_closed_payroll_dates;
    @payDates = @$payDates_ref;

	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body onload='startUp1()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3> Open Payroll Dates</h3>
	<div id='box1'>
	Employee Number:<br/><br/>
	Employee Name:<br/><br/>
	Select Date:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_pay_open_dates.pl' method='post'>
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<select id=payDate name="payDate" tabindex=1 >
ENDHEADER
foreach $date (@payDates) {
   print "<option value=$date >$date</option>";
}
print <<ENDHEADER;
</select>
<br/><br/>
<input type='submit' name='submit' value='Open Dates'>
<br/><br/>
<a href="${cgi_ref}trak_00_pay_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
<br/><br/>
</form>
</body>
</html>

ENDHEADER

}



